package kr.co.doriclassic.chapter17;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;

import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JPasswordField;
import javax.swing.JFormattedTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.ParseException;

public class MyFrame extends JFrame implements ActionListener {
	private JTextField tfId;
	private JPasswordField pfPassword;
	private JFormattedTextField ftfDate;
	private JTextArea textArea;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MyFrame frame = new MyFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws ParseException 
	 */
	public MyFrame() throws ParseException {
		initialize();
	}
	private void initialize() throws ParseException {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 370);
		JPanel contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JPanel panelTop = new JPanel();
		contentPane.add(panelTop, BorderLayout.NORTH);
		panelTop.setLayout(new GridLayout(0, 2, 0, 0));
		
		JLabel lblNewLabel = new JLabel("ID : ");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		panelTop.add(lblNewLabel);
		
		tfId = new JTextField();
		tfId.addActionListener(this);
		panelTop.add(tfId);
		tfId.setColumns(30);
		
		JLabel lblNewLabel_1 = new JLabel("Password : ");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
		panelTop.add(lblNewLabel_1);
		
		pfPassword = new JPasswordField();
		pfPassword.addActionListener(this);
		pfPassword.setColumns(30);
		panelTop.add(pfPassword);
		
		JLabel lblNewLabel_2 = new JLabel("Date : ");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
		panelTop.add(lblNewLabel_2);
		
		MaskFormatter mf = new MaskFormatter("####.##.##");
		mf.setPlaceholderCharacter('_');
		ftfDate = new JFormattedTextField(mf);
		ftfDate.addActionListener(this);
		panelTop.add(ftfDate);
		
		JPanel panelCenter = new JPanel();
		contentPane.add(panelCenter, BorderLayout.CENTER);
		
		textArea = new JTextArea(10, 30);
		textArea.setEditable(false);
		JScrollPane scrollPane = new JScrollPane(textArea);
		panelCenter.add(scrollPane);
		
		
		
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == tfId||e.getSource() == pfPassword||e.getSource() == ftfDate) {
			StringBuffer sb = new StringBuffer();
			sb.append("id = " + tfId.getText()+"\n");
			sb.append("password = " + pfPassword.getText()+"\n");
			sb.append("Date = " + ftfDate.getText()+"\n");
			
			textArea.append(sb.toString());
			tfId.selectAll();
			tfId.setText("");
			pfPassword.selectAll();
			pfPassword.setText("");
			ftfDate.selectAll();
			ftfDate.setValue("");
			tfId.requestFocus();
			
			textArea.setCaretPosition(textArea.getDocument().getLength());
		}
	}	
}
